/*
 * @Author: llixxm
 * @Date: 2020-09-16 14:09:12
 * @Description: 表格公共事件
 */
import { cardCache } from 'nc-lightapp-front';
import { CARD_IDS, DATA_SOURCE, COPY } from '../../consts';
let { setDefData, getDefData } = cardCache;

/**
 * @description: 表格行点击事件
 */
let tableButtonClick = (props, key, text, record, index, tableId, page) =>{
    switch (key) {
        // 表格操作按钮
        case 'copy_inner': //复制
            let copyData = [record];
            copyLine(page, true, copyData);
            break;
        case 'delete_inner': //删行
            delLines(page, [index]);
            break;
        case 'paste_inner': //粘贴至此
            pasteline(page, index);
            break;
        case 'insert_inner': //插入行
            props.cardTable.insertDataByIndexs(tableId, [{index: index, data: {}}]);
            break;
        default:
            break;
    } 
};
/**
 * @description: 获取选中行数据
 * @return {type} //rowData: 行数据, indexs:行索引
 */
let getCheckedRows = (page) =>{
    let rows = page.props.cardTable.getCheckedRows(CARD_IDS.TABLE_ID);
    let rowData = [], indexs = [];
    if(rows){
        rows.map((item) => {
            rowData.push(item.data)
            indexs.push(item.index)
        });
        return {rowData, indexs};
    }
}
/**
 * @description: 控制表格肩部按钮显示
 * @param {type} isCopying: 是否正在复制中 true/false
 */
let cardBodyControl = (page, isCopying) => {
    page.props.button.setButtonVisible(['paste2endline', 'cancel_copy'], isCopying);
    page.props.button.setButtonVisible(['addLine', 'copyLine', 'delLine'], !isCopying);
}

/**
 * @description: 复制
 * @param {type} copyData: 复制数据
 */
let copyLine = (page, isCopying, copyData) => {
    setDefData(COPY, DATA_SOURCE, copyData);
    updataCopyStatus(page, isCopying)
}

/**
 * @description: 更新复制状态
 * @param {type} 
 */
let updataCopyStatus = (page, isCopying) => {
    page.setState({
        isCopying: isCopying
    }, () => {
        cardBodyControl(page, isCopying);
    })
}
/**
 * @description: 粘贴
 * @param {type} pasteIndex: 需要粘贴的位置 
 */
let pasteline = (page, pasteIndex) => {
    let pasteData = getDefData(COPY, DATA_SOURCE);
    let tempData = JSON.parse(JSON.stringify(pasteData));
    tempData.map((item)=>{
        item.values.pk_transferdetail = {display: '', value: ''} //清空行主键
        item.values.ts = {display: '', value: ''} 
    })
    page.props.cardTable.insertRowsAfterIndex(page.tableId, tempData, pasteIndex - 1);
}
/**
 * @description: 删行
 * @param {type} indexArr: 删除行角标 arr
 */
let delLines = (page, indexs) => {
    let indexArr = indexs && indexs.length > 0 ? indexs : getCheckedRows(page).indexs;
    if(indexArr){
        page.props.cardTable.delRowsByIndex(CARD_IDS.TABLE_ID, indexArr);
        page.updateDelLineBtn();
    }
}


export { getCheckedRows, tableButtonClick, cardBodyControl, copyLine, updataCopyStatus, pasteline, delLines }